﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

namespace Might.UIControls.PropertyEditor.Items {
    public partial class ColorComboBoxItem : UserControl , IPropertyEditorItemWithValue<Color?> {

        public event EventHandler ValueChanged;
        
        private string label;
        private Color? value;
        private Dictionary< Color , ComboBoxItem > comboBoxItems = new Dictionary<Color,ComboBoxItem>();
        private bool raiseEvents = false;
        
        public ColorComboBoxItem( string label ) {
            InitializeComponent();
            this.label = label;
            labelTxt.Text = label + " : ";

            PropertyInfo[] props = typeof( Colors ).GetProperties();

            foreach( PropertyInfo prop in props ) {
            
                Color clr = (Color)prop.GetValue( null , null );
                bool isBlack = .222 * clr.R + .707 * clr.G + .071 * clr.B > 128;

                ComboBoxItem item = new ComboBoxItem();
                item.Background = new SolidColorBrush( clr );
                item.Content = new TextBlock() { Text = prop.Name };
                item.Foreground = isBlack ? new SolidColorBrush( Colors.Black ) : new SolidColorBrush( Colors.White ); 
                item.Margin = new Thickness( 15 , 0 , 0 , 0 );
                item.Width = 100;
                item.Tag = clr;
                
                colorComboBox.Items.Add( item );
                
                comboBoxItems.Add( clr , item );
                
            }

            colorComboBox.SelectionChanged += new SelectionChangedEventHandler( colorComboBox_SelectionChanged );
            
        }

        public Color? Value {
            get {    
                return value;
            }
            set {
                raiseEvents = false;
                this.value = value;
                if( value == null ) {
                    colorComboBox.SelectedItem = null;
                } else {
                    
                    if( comboBoxItems.ContainsKey( value.Value ) ) {
                        colorComboBox.SelectedItem = comboBoxItems[ value.Value ];                        
                    } else {
                        colorComboBox.SelectedItem  = null;
                    }
                                        
                }
                raiseEvents = true;
            }
        }        

        public string Label { get { return label; } }

        protected void onValueChanged( EventArgs e ) {
            if( ValueChanged != null && raiseEvents ) {
                if( colorComboBox.SelectedItem == null ) { 
                    value = null;
                } else {
                    value = (Color)( colorComboBox.SelectedItem as ComboBoxItem ).Tag;
                }
                ValueChanged( this , e );
            }
        }

        void colorComboBox_SelectionChanged( object sender , SelectionChangedEventArgs e ) {            
            //Color clr = (Color)( colorComboBox.SelectedItem as ComboBoxItem ).Tag;
            //bool isBlack = .222 * clr.R + .707 * clr.G + .071 * clr.B > 128;            
            //colorComboBox.Background = new SolidColorBrush( clr );
            //colorComboBox.Foreground = isBlack ? new SolidColorBrush( Colors.Black ) : new SolidColorBrush( Colors.White );                        
            onValueChanged( null );
        }

    }
}
